package com.bridgeintelligent.tag.webserver.dim.mgmt.service.datamonitoring;


import com.bridgeintelligent.tag.webserver.dim.mgmt.api.BaseCode;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimRep;
import com.bridgeintelligent.tag.webserver.dim.mgmt.service.assess.IAssessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * @description 标签数据监控
 * @author: xiayan 2020-10-13
 **/
@Api(tags = "数据监控")
@RestController
@AllArgsConstructor
@RequestMapping("/api/datamonitoring")
@Validated
public class DatamonitoringController {

    private DatamonitoringService datamonitoringService;
    @ApiOperation(value = "查询标签监控数据", notes = "聚合信息")
    @GetMapping("query/{dimId}")
    public DimRep query(@NotBlank @PathVariable String dimId){
        return new DimRep<>(datamonitoringService.getInfo(dimId), BaseCode.SUCCESS);
    }
    @ApiOperation(value = "标签使用下载等统计")
    @GetMapping("statistics/{dimId}")
    public DimRep statistics(@NotBlank @PathVariable String dimId){
        return new DimRep<>(datamonitoringService.getStatistics(dimId), BaseCode.SUCCESS);
    }
    @ApiOperation(value = "标签问题及描述")
    @GetMapping("detailed/{dimId}")
    public DimRep detailed(@NotBlank @PathVariable String dimId,@NotBlank  String star){
        return new DimRep<>(datamonitoringService.getDetailed(dimId,star), BaseCode.SUCCESS);
    }
    @ApiOperation(value = "星级占比统计")
    @GetMapping("statcount/{dimId}")
    public DimRep statcount(@NotBlank @PathVariable String dimId){
        return new DimRep<>(datamonitoringService.getStatcount(dimId), BaseCode.SUCCESS);
    }
    @ApiOperation(value = "评价标签统计")
    @GetMapping("commenttj/{dimId}")
    public DimRep commenttj(@NotBlank @PathVariable String dimId,@NotBlank  String star){
        return new DimRep<>(datamonitoringService.getComment(dimId,star), BaseCode.SUCCESS);
    }
    @ApiOperation(value = "标签历史统计")
    @GetMapping("history/{dimId}")
    public DimRep history(@NotBlank @PathVariable String dimId){
        return new DimRep<>(datamonitoringService.getHistory(dimId), BaseCode.SUCCESS);
    }
    public static List<String> getMonthBetween(String minDate, String maxDate) throws Exception {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(minDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(maxDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }

        return result;
    }



        /*public static void main(String[] args) throws Exception {
            Calendar ca = Calendar.getInstance();
            ca.setTime(new Date());
            ca.add(Calendar.MONTH, -1);
            Date now = ca.getTime();
            ca.add(Calendar.YEAR, -1);
            Date lastMonth = ca.getTime();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            List<String> list= getMonthBetween(sf.format(lastMonth),sf.format(now));
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
            }
            System.out.println(sf.format(now));
            System.out.println(sf.format(lastMonth));
        }*/
  /*  public List<NearlyAMonth> compareTime(List<NearlyAMonth> nearlyAMonth,List<String> dates){
        List<String> list=new ArrayList<>();
        nearlyAMonth.forEach(item->list.add(item.getTime()));
        dates.forEach(item->{
            if(!list.contains(item)){
                NearlyAMonth nearly= new NearlyAMonth();
                nearly.setTime(item);
                nearlyAMonth.add(statistics);
            }
        });
        Collections.sort(coStatistics, new Comparator<CoStatistics>() {
            @Override
            public int compare(CoStatistics o1, CoStatistics o2) {
                if(o1.getTime().compareTo(o2.getTime())==0){
                    return -1;
                }
                return o1.getTime().compareTo(o2.getTime());
            }
        });
        return nearlyAMonth;
    }*/

}
